﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;

namespace scroll_ad
{
    public delegate void MyDelegate(SyncInfo info);
    public delegate void RunScroll(bool ebabled);
    public partial class App : Form
    {
        private AD winAD;
        public MyDelegate MyEvent = null;
        public RunScroll runScroll = null;
        private SyncInfo info = new SyncInfo();
        private bool isRun = false;
        public App()
        {
            InitializeComponent();
            this.winAD = new AD(ref MyEvent, ref runScroll);
            this.winAD.Show();
            this.speed.MouseWheel += new MouseEventHandler(speedMouseWheel);
         
        }
        private void speedMouseWheel(object sender, MouseEventArgs e) {

            int value = 0;
            try
            {

                value = int.Parse(this.speed.Text.ToString());
                if (e.Delta > 0)
                {
                value += 1;
                }
                else
                {
                    value -= 1;
                }
                this.speed.Text = value.ToString();
            }
            catch (Exception ex)
            {
                
            }
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void runButton_Click(object sender, EventArgs e)
        {
            this.isRun = !this.isRun;
            this.runScroll(this.isRun);

            if (this.isRun)
            {
                this.runButton.Text = "暂停";
            }
            else
            {
                this.runButton.Text = "运行";
            }
        }

        private void initInfo()
        {
            this.info.readCfg();
            this.speed.Text = this.info.speed.ToString();
            this.ScrollRichText.Text = this.info.text;
            this.adBgColor.BackColor = this.info.adBg;
            this.fontColor.BackColor = this.info.fontColor;
            this.fontBgColor.BackColor = this.info.fontBgColor;
        }
        private void syncInfoToAd()
        {
            if (!this.info.init) return;

            this.info.speed = int.Parse(this.speed.Text);
            this.info.text = this.ScrollRichText.Text;
            this.info.adBg = this.adBgColor.BackColor;
            this.info.fontColor = this.fontColor.BackColor;
            this.info.fontBgColor = this.fontBgColor.BackColor;
            MyEvent(info);
            this.info.saveCfg();
        }
        private void adBgColor_Click(object sender, EventArgs e)
        {
            if (this.colorDialog.ShowDialog() == DialogResult.OK)
            {
                this.adBgColor.BackColor = this.colorDialog.Color;
                this.syncInfoToAd();
            }

        }

        private void fontColor_Click(object sender, EventArgs e)
        {
            if (this.colorDialog.ShowDialog() == DialogResult.OK)
            {
                this.fontColor.BackColor = this.colorDialog.Color;
                this.syncInfoToAd();
            }

        }
        private void fontBgColor_Click(object sender, EventArgs e)
        {
            if (this.colorDialog.ShowDialog() == DialogResult.OK)
            {
                this.fontBgColor.BackColor = this.colorDialog.Color;
                this.syncInfoToAd();
            }
        }

        private void changeFontBtn_Click(object sender, EventArgs e)
        {
            this.fontDialog.Font = this.info.font;
            if (this.fontDialog.ShowDialog() == DialogResult.OK)
            {
                this.info.font = this.fontDialog.Font;
                this.syncInfoToAd();
            }
        }

        private void ScrollRichText_TextChanged(object sender, EventArgs e)
        {
            this.syncInfoToAd();
        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void speed_TextChanged(object sender, EventArgs e)
        {
            try
            {
                int speed = int.Parse(this.speed.Text);
                this.speed.Text = speed.ToString();
            }
            catch (Exception ex)
            {
                this.speed.Text = "20";
            }

            this.syncInfoToAd();
        }

        private void App_Load(object sender, EventArgs e)
        {
            this.info.init = false;
            this.initInfo();
            this.info.init = true;
            this.syncInfoToAd();
            this.autoRun.Checked = this.isAutoStart();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("iexplore.exe", "https://gitee.com/tidys/scrolling-ad/blob/master/README.md");
        }

        private void App_KeyPress(object sender, KeyPressEventArgs e)
        {
        }

        private void ScrollRichText_KeyPress(object sender, KeyPressEventArgs e)
        {

          
        }

        private void ScrollRichText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData== Keys.V && Control.ModifierKeys == Keys.Control)
            {
            }
        }


        private string appKey = "广告滚动小助手";
        private string autoRunRegistryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";

        private bool isAutoStart()
        {
            RegistryKey subKey = Registry.CurrentUser.OpenSubKey(autoRunRegistryKey);
            object key = subKey.GetValue(appKey);
            if (key!=null && key.Equals(Application.ExecutablePath))
            {
                return true;
            }
            return false;
        }
        private void AutoStart(bool isAuto=true, bool showInfo = true)
        {
            try
            {
                RegistryKey subKey = Registry.CurrentUser.CreateSubKey(autoRunRegistryKey);
            
                if (isAuto)
                {
                    subKey.SetValue(appKey, Application.ExecutablePath);
                }
                else
                {
                    subKey.DeleteValue(appKey, false);
                }
                subKey.Close();
                Registry.CurrentUser.Close();
            }
            catch (Exception)
            {
                if (showInfo)
                {
                    MessageBox.Show("需要以管理员权限才能自启动", "提示");
                }
            }
        }



        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.autoRun.Checked != this.isAutoStart())
            {
                this.AutoStart(this.autoRun.Checked, true);
            }
        }
    }
    public class SyncInfo
    {
        public bool init = true;// 是否在初始化
        public int speed = 1;
        public String text="请输入滚动文本。。。";
        public Font font = new Font("宋体", 26);
        public Color adBg = Color.White;
        public Color fontBgColor = Color.White;
        public Color fontColor = Color.Black;

        public void readCfg()
        {
            this.speed = Properties.Settings.Default.Speed;
            this.font = Properties.Settings.Default.Font;
            this.text = Properties.Settings.Default.AdText;
            this.adBg = Properties.Settings.Default.WinBgColor;
            this.fontBgColor = Properties.Settings.Default.FontBgColor;
            this.fontColor = Properties.Settings.Default.FontColor;
        }

        public void saveCfg()
        {
            Properties.Settings.Default.Speed = this.speed;
            Properties.Settings.Default.Font = this.font;
            Properties.Settings.Default.AdText = this.text;
            Properties.Settings.Default.WinBgColor = this.adBg;
            Properties.Settings.Default.FontBgColor = this.fontBgColor;
            Properties.Settings.Default.FontColor = this.fontColor;
            Properties.Settings.Default.Save();
        }
    }
}
